package com.example.restaurant.dao;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.restaurant.model.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.restaurant.service.dto.FeiDishDto;
import com.example.restaurant.service.dto.GetStoreNameDto;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 用户表 Mapper 接口
 * </p>
 *
 * @author x
 * @since 2022-09-29
 */
public interface UserMapper extends BaseMapper<User> {

    //通过userID查询角色ID，再查询对应角色
    @Select("select role_name from user u join role r ON u.role_id=r.id WHERE u.id = #{userId}")
    List<String> getRole(@Param("userId") int userId);

    @Select("select permission_name from `user` u join role r ON u.role_id=r.id \n" +
            "join role_per rp ON r.id = rp.role_id\n" +
            "join permission p ON p.id = rp.permission_id\n" +
            "WHERE u.id = #{userId}")
    List<String> getPermissions(@Param("userId") int userId);


    @Select("<script>SELECT u.id,u.username,u.register_time,u.status,u.telephone,u.role_id,u.password FROM user_store ust JOIN `user` u ON u.id=ust.user_id   <where>\n" +
            "            <if test=\"username!=null||username!=''\">\n" +
            "                u.username LIKE #{username}\n" +
            "            </if>\n" +
            "\n" +
            "        </where>\n" +
            "        AND ust.store_id=#{storeId} AND u.role_id=2 </script>")
    IPage<GetStoreNameDto> getStoreName(Page<GetStoreNameDto> page, @Param("storeId") int storeId, @Param("username")String username);



}
